package com.ohuang.myapp.provider;

import com.ohuang.baseapp.utils.HiLogUtil;
import com.ohuang.baseapp.view.listContainer.ListContainerProvider;
import com.ohuang.baseapp.view.listContainer.ListContainerProvider2;
import com.ohuang.myapp.ResourceTable;
import com.ohuang.myapp.bean.TieBa;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ListProvider2 extends ListContainerProvider2 {
    private List<TieBa> list;
    private Context context;


    public ListProvider2(List<TieBa> list, Context context) {
        this.list = list;
        this.context = context;
    }


    public void setList(List<TieBa> list) {
        this.list = list;
    }

    @Override
    public ListContainerProvider.ViewHolder onCreateViewHolder(ComponentContainer parent, int viewType, int p) {

        if (viewType == -1) {
            return new ImageHolder(LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_img, null, false));
        }
        if(viewType==1){
            return new TextHolder(LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_text, null, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ListContainerProvider.ViewHolder holder, int position) {
        if (holder instanceof TextHolder) {
            ((TextHolder) holder).text.setText(list.get(position).getContent());
        }
        HiLogUtil.error("list"," p="+position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public int getItemComponentType(int position) {
        if (position % 9 == 0) {
            return -1;
        } else {
            return 1;
        }
    }



//    @Override
//    public int getComponentTypeCount() {
//        return 2;
//    }

    public static class TextHolder extends ListContainerProvider.ViewHolder {
        Text text;

        public TextHolder(Component itemComponent) {
            super(itemComponent);
            text = (Text) itemComponent.findComponentById(ResourceTable.Id_text);
        }
    }

    public static class ImageHolder extends ListContainerProvider.ViewHolder {
        Image image;

        public ImageHolder(Component component) {
            super(component);
            image = (Image) component.findComponentById(ResourceTable.Id_image);
        }
    }
}
